using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL
{
    public class RequestSlots
    {
        public Slot[] Slots;
        int iCapacity;
        int iNextSlot;
        int iRequestCount;
        int iPendingCount;
        
        public RequestSlots(int piCapacity)
        {
            Slots = new Slot[piCapacity];
            for (int iIdx = 0; iIdx < piCapacity; iIdx++)
                Slots[iIdx] = new Slot();

            iNextSlot = 0;
            iCapacity = piCapacity;
            iRequestCount = 0;
            iPendingCount = 0;
        }

        public int RequestSlot(int piID)
        {
            int iSlot = iNextSlot;
            
            Slots[iSlot].ID = piID;
            Slots[iSlot].Ordered = true;
            Slots[iSlot].Data = 0;
            Slots[iSlot].Fulfilled = false;

            iNextSlot++;
            if (iNextSlot >= iCapacity)
                iNextSlot = 0;

            iRequestCount++;
            iPendingCount++;
            return iSlot;
        }

        public bool FulfillSlot(int piSlot, int piData)
        {
            if (piSlot >= iCapacity)
                return false;

            Slots[piSlot].Data = piData;
            Slots[piSlot].Ordered = false;
            Slots[piSlot].Fulfilled = true;

            iPendingCount--;
            return true;
        }

        public int RequestCount
        {
            get { return iRequestCount; }
        }

        public int PendingCount
        {
            get { return iPendingCount; }
        }
    }
}
